package com.tywj.buscustomerapp.utils;

/**
 * Created by newbiechen on 2018/1/1.
 */

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *@Description: 将字符串转化为MD5
 */

public class MD5Utils {

    public static String getUrl(String type){
        String code = "";
        if (type.equals("1")){
            try {
//                code = URLEncoder.encode("http://beta.cd917.com/app/main.html#/tab/home/",   "utf-8");
                code = URLEncoder.encode("http://www.cd917.com/app/main.html#/tab/home/",   "utf-8");
                return code;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }else if (type.equals("2")){
            try {
//                code = URLEncoder.encode("http://beta.cd917.com/app/main.html#/tab/orderList",   "utf-8");
                code = URLEncoder.encode("http://www.cd917.com/app/main.html#/tab/orderList",   "utf-8");
                return code;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return code;
    }

    public static String getSign(String tel,String apiKey,String encodeUrl,String recommendCode){
        String secret = "60B7F841267C41FD92FDB78C15B7C615";
//        String secret = "FBED2885DE294C7BBB9EA6408E8EEBD5";//测试

        String result = secret+"apiKey"+apiKey+"recommendCode"+recommendCode+"ReturnUrl"+encodeUrl+"Tel"+tel+secret;
        result = result.toUpperCase();
        result = encode(result);
        return result;
    }

    private static String encode(String str) {
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");//
            byte[] digest = instance.digest(str.getBytes());//
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                int i = b & 0xff;
                String hexString = Integer.toHexString(i);//
                if (hexString.length() < 2) {
                    hexString = "0" + hexString;
                }
                sb.append(hexString);
            }
            return sb.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String strToMd5By32(String str){
        String reStr = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(str.getBytes());
            StringBuffer stringBuffer = new StringBuffer();
            for (byte b : bytes){
                int bt = b&0xff;
                if (bt < 16){
                    stringBuffer.append(0);
                }
                stringBuffer.append(Integer.toHexString(bt));
            }
            reStr = stringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return reStr;
    }

    public static String strToMd5By16(String str){
        String reStr = strToMd5By32(str);
        if (reStr != null){
            reStr = reStr.substring(8, 24);
        }
        return reStr;
    }
}
